package com.itheima;

import com.itheima.domain.Classes;
import com.itheima.domain.Student;
import com.itheima.mapper.ClassesMapper;
import com.itheima.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ClassesTest {
    private SqlSession sqlSession;

    @Before
    public void init() throws IOException {
        // 加载核心配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapperConfig.xml");

        // 获取SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        // 获取SqlSessionFactory
        SqlSessionFactory factory = builder.build(in);

        // 获取连接SqlSession
        sqlSession = factory.openSession();
    }

    @After
    public void close() {
        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    @Test
    public void findAll(){
        ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class);

        List<Classes> classes = mapper.findAllClasses();
        for (Classes aClass : classes) {
            System.out.println(aClass);
        }
    }
}
